/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.system.domain.CompanyConfig;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.CompanyConfigRepository;
import me.zhengjie.modules.system.service.CompanyConfigService;
import me.zhengjie.modules.system.service.dto.CompanyConfigDto;
import me.zhengjie.modules.system.service.dto.CompanyConfigQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.CompanyConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wangyu
* @date 2025-09-24
**/
@Service
@RequiredArgsConstructor
public class CompanyConfigServiceImpl implements CompanyConfigService {

    private final CompanyConfigRepository companyConfigRepository;
    private final CompanyConfigMapper companyConfigMapper;

    @Override
    public Map<String,Object> queryAll(CompanyConfigQueryCriteria criteria, Pageable pageable){
        Page<CompanyConfig> page = companyConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(companyConfigMapper::toDto));
    }

    @Override
    public List<CompanyConfigDto> queryAll(CompanyConfigQueryCriteria criteria){
        return companyConfigMapper.toDto(companyConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CompanyConfigDto findById(Long id) {
        CompanyConfig companyConfig = companyConfigRepository.findById(id).orElseGet(CompanyConfig::new);
        ValidationUtil.isNull(companyConfig.getId(),"CompanyConfig","id",id);
        return companyConfigMapper.toDto(companyConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CompanyConfigDto create(CompanyConfig resources) {
        if(companyConfigRepository.findByCode(resources.getCode()) != null){
            throw new EntityExistException(CompanyConfig.class,"code",resources.getCode());
        }
        return companyConfigMapper.toDto(companyConfigRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CompanyConfig resources) {
        CompanyConfig companyConfig = companyConfigRepository.findById(resources.getId()).orElseGet(CompanyConfig::new);
        ValidationUtil.isNull( companyConfig.getId(),"CompanyConfig","id",resources.getId());
        CompanyConfig companyConfig1 = companyConfigRepository.findByCode(resources.getCode());
        if(companyConfig1 != null && !companyConfig1.getId().equals(companyConfig.getId())){
            throw new EntityExistException(CompanyConfig.class,"code",resources.getCode());
        }
        companyConfig.copy(resources);
        companyConfigRepository.save(companyConfig);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            companyConfigRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CompanyConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CompanyConfigDto companyConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("公司名称", companyConfig.getName());
            map.put("编码", companyConfig.getCode());
            map.put("状态", companyConfig.getStatus());
            map.put("创建时间", companyConfig.getCreateTime());
            map.put("更新时间", companyConfig.getUpdateTime());
            map.put("创建人", companyConfig.getCreateby());
            map.put("更新人", companyConfig.getUpdateby());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}